<template>
  <div ref="chart" :style="chartStyle"></div>
</template>

<script>
import echarts from 'echarts';

export default {
  name: 'barcircle3',
  props: {
    config: {
      required: true,
    },
    chartStyle: {
      require: false,
      type: Object,
      default: () => ({
        height: '100%',
        position: 'absolute',
        width: '100%',
        top: '0px',
        bottom: '0px',
        left: '0px',
      }),
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {

  },
  mounted() {
    this.renderChart();
  },
  methods: {
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    renderChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chart, this.config.theme);
      }
      const option = this.makeOptions(this.config, this.chartData);
      this.chart.clear();
      this.chart.setOption(option);
      setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    makeOptions() {
      const data1 = [
        {
          name: '谷类',
          value: 175.17,
        },
        {
          name: '豆类',
          value: 148.35,
        },
        {
          name: '油料作物',
          value: 95.36,
        },
        {
          name: '中草药材',
          value: 56,
        },
        {
          name: '蔬菜及食用菌',
          value: 45,
        },
        {
          name: '瓜果类',
          value: 34,
        },
        {
          name: '其他作物',
          value: 95.36,
        },
      ];
      const xAxisData = [];
      const seriesData1 = [];
      const barTopColor = ['#02c3f1', '#53e568', '#02c3f1', '#53e568', '#02c3f1', '#53e568', '#02c3f1', '#53e568'];
      const barBottomColor = [
        'rgba(2,195,241,0.1)',
        'rgba(83, 229, 104, 0.1)',

        'rgba(2,195,241,0.1)',
        'rgba(83, 229, 104, 0.1)',

        'rgba(2,195,241,0.1)',
        'rgba(83, 229, 104, 0.1)',

        'rgba(2,195,241,0.1)',
        'rgba(83, 229, 104, 0.1)',
      ];
      data1.forEach((item) => {
        xAxisData.push(item.name);
        seriesData1.push(item.value);
      });
      const option = {
        backgroundColor: '#000',
        grid: {
          top: '20%',
          bottom: '100',
          left: '50',
          right: '100',
          containLabel: false,
        },
        xAxis: {
          data: xAxisData,
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: true,
            margin: 25,
            rotate: 25,
            align: 'center',
            textStyle: {
              fontSize: 12,
              color: '#ffffff',
            },
          },
          interval: 0,
        },
        yAxis: {
          type: 'value',
          // name: '件',
          nameTextStyle: {
            color: '#FFFFFF',
            fontSize: 12,
          },
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
        },
        series: [
          {
            name: '柱顶部',
            type: 'pictorialBar',
            symbolSize: [26, 10],
            symbolOffset: [0, -5],
            z: 12,
            itemStyle: {
              normal: {
                color(params) {
                  return barTopColor[params.dataIndex];
                },
              },
            },
            label: {
              show: false,
              position: 'top',
              fontSize: 16,
            },
            symbolPosition: 'end',
            data: seriesData1,
          },
          {
            name: '柱底部',
            type: 'pictorialBar',
            symbolSize: [26, 10],
            symbolOffset: [0, 5],
            z: 12,
            itemStyle: {
              normal: {
                color(params) {
                  return barTopColor[params.dataIndex];
                },
              },
            },
            data: seriesData1,
          },
          {
            type: 'bar',
            itemStyle: {
              normal: {
                color(params) {
                  return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                      offset: 1,
                      color: barTopColor[params.dataIndex],
                    },
                    {
                      offset: 0,
                      color: barBottomColor[params.dataIndex],
                    },
                  ]);
                },
                opacity: 0.8,
              },
            },
            z: 16,
            silent: true,
            barWidth: 26,
            barGap: '-100%', // Make series be overlap
            data: seriesData1,
          },
        ],
      };
      return option;
    },
  },
  watch: {
    config: {
      deep: true,
      handler() {
        if (this.config.needResize) {
          this.config.needResize = false;
          this.handleResize();
        }
      },
    },
  },
};
</script>

<style scoped lang="stylus"></style>
